﻿using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Writer.Api.Application.Command.OutSourOrderModel;
using MediatR;

namespace B.S.Outsourcing.Writer.Api.Application.CommandHandler.OutSourOrderModel
{
    public class UpdateOutSourOrderCommandHandler : IRequestHandler<UpdateOutSourOrderCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OutsourcingOrderModel> baseRepository;
        private readonly DBHelper1 dBHelper;

        public UpdateOutSourOrderCommandHandler(IBaseRepository<OutsourcingOrderModel> baseRepository, DBHelper1 dBHelper)
        {
            this.baseRepository = baseRepository;
            this.dBHelper = dBHelper;
        }

        public async Task<ApiResult<int>> Handle(UpdateOutSourOrderCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            request.RequirementDate = request.RequirementDate.Value.AddHours(8);
            var sql = $"update OutsourcingOrderModel set DocumentCode='{request.DocumentCode}',BusinessType='{request.BusinessType}',PlanOrderNumber='{request.PlanOrderNumber}',MaterialCode='{request.MaterialCode}',Supplier='{request.Supplier}',DocumentDate='{request.DocumentDate}',Operator='{request.Operator}',SalesOrderNumber='{request.SalesOrderNumber}',MaterialName='{request.MaterialName}',EntrustedContract='{request.EntrustedContract}',DocumentStatus='{request.DocumentStatus}',Auditor='{request.Auditor}',OrderDeliveryDate='{request.OrderDeliveryDate}',SpecificationModel='{request.SpecificationModel}',OutsourcingQuantity='{request.OutsourcingQuantity}',BusinessStatus='{request.BusinessStatus}',AuditDate='{request.AuditDate}',RequirementDate='{request.RequirementDate}',DrawingNumber='{request.DrawingNumber}',ProcessingFee='{request.ProcessingFee}',Remarks='{request.Remarks}',CreatedDate='{request.CreatedDate}',CreatedBy='{request.CreatedBy}',ModifiedDate='{request.ModifiedDate}',ModifiedBy='{request.ModifiedBy}',IsDelete='{request.IsDelete}' where id='{request.Id}'";
            var str = dBHelper.NonQuery(sql);
            if (str > 0)
            {
                result.Code = ApiEnums.成功;
                result.Message = "成功";
                result.Data = str;
            }
            else
            {
                result.Code = ApiEnums.失败;
                result.Message = "失败";
                result.Data = str;
            }
            return await Task.FromResult(result);
        }
    }
}
